

package cn.shengchao.controler;

import cn.shengchao.util.helper.LoginHelper;
import cn.shengchao.examstar.me.collect.domain.CourseCollect;
import cn.shengchao.examstar.me.collect.ohs.local.appservices.CollectAppService;
import com.google.common.collect.Maps;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.continew.starter.web.model.R;

import java.util.List;

@RestController
@Slf4j
@CrossOrigin("${project.cross-origin}")
@RequestMapping("/api/${project.version}/collect")
public class CollectController {

    @Autowired
    private CollectAppService collectAppService;

    @Operation(description = "收藏")
    @GetMapping("course/{courseId}/isCollect")
    public R isCollect(@PathVariable Long courseId) {
        Long userId = LoginHelper.getUserId();
        return R.ok(Maps.immutableEntry("isCollect", collectAppService.isCollect(userId, courseId)));
    }

    @Operation(description = "收藏")
    @PostMapping("course/{courseId}")
    public R collect(@PathVariable Long courseId) {
        Long userId = LoginHelper.getUserId();
        collectAppService.collect(userId, courseId);
        return R.ok();
    }

    @Operation(description = "取消收藏")
    @DeleteMapping("course/{courseId}")
    public R cancelCollect(@PathVariable Long courseId) {
        Long userId = LoginHelper.getUserId();
        collectAppService.cancelCollect(userId, courseId);
        return R.ok();
    }

    @Operation(description = "我收藏的课程")
    @GetMapping("course")
    public List<Long> myCollect() {
        Long userId = LoginHelper.getUserId();
        log.info("获取我的收藏：{}", userId);
        List<CourseCollect> courseCollects = collectAppService.getCollect(userId);
        return courseCollects.stream().map(CourseCollect::getCourseId).toList();
    }

    @Operation(description = "关注老师")
    @GetMapping("teacher/{teacherId}/isCollect")
    public R isTeacherCollect(@PathVariable Long teacherId) {
        Long userId = LoginHelper.getUserId();
        return R.ok(Maps.immutableEntry("isCollect", collectAppService.isCollect(userId, teacherId)));
    }

    @Operation(description = "关注老师")
    @PostMapping("teacher/{teacherId}")
    public R collectTeacher(@PathVariable Long teacherId) {
        Long userId = LoginHelper.getUserId();
        collectAppService.collectTeacher(userId, teacherId);
        return R.ok();
    }

    @Operation(description = "取消关注老师")
    @DeleteMapping("teacher/{teacherId}")
    public R cancelCollectTeacher(@PathVariable Long teacherId) {
        Long userId = LoginHelper.getUserId();
        collectAppService.cancelCollectTeacher(userId, teacherId);
        return R.ok();
    }

}
